﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EHR.Elements
{
    public partial class ByDateQuerybar : UserControl
    {
        public event EventHandler<DateEventArgs> Query;

        public DateTime Begin
        {
            get { return (DateTime)dateBegin.SelectedDate; }
            set { dateBegin.SelectedDate = value; }
        }

        public DateTime End
        {
            get { return (DateTime)dateEnd.SelectedDate; }
            set { dateEnd.SelectedDate = value; }
        }

        public string Employee
        {
            get { return txtEmployee.Text; }
            set { txtEmployee.Text = value; }
        }

        public ByDateQuerybar()
        {
            InitializeComponent();

            dateBegin.DisplayDateStart = new DateTime(1900, 1, 1);
            dateBegin.DisplayDateEnd = new DateTime(2999, 12, 31);
            dateEnd.DisplayDateStart = new DateTime(1900, 1, 1);
            dateEnd.DisplayDateEnd = new DateTime(2999, 12, 31);

            dateBegin.SelectedDate = new DateTime(DateTime.Today.Year, 1, 1);
            dateEnd.SelectedDate = DateTime.Today;
        }

        public bool Validate()
        {
            txtEmployee.Text = txtEmployee.Text.Trim();

            if (dateBegin.SelectedDate == null)
            {
                MessageBox.Show("请输入开始时间！");
                dateBegin.Focus();
                return false;
            }

            if (dateEnd.SelectedDate == null)
            {
                MessageBox.Show("请输入截止时间！");
                dateBegin.Focus();
                return false;
            }


            DateTime begin = (DateTime)dateBegin.SelectedDate;
            DateTime end = (DateTime)dateEnd.SelectedDate;
            DateTime minBegin = new DateTime(1900, 1, 1);

            if (begin < minBegin)
            {
                MessageBox.Show("开始时间不能小于1900-1-1");
                dateBegin.Focus();
                return false;
            }

            if (begin > end)
            {
                MessageBox.Show("开始时间不能大于截止时间！");
                dateBegin.Focus();
                return false;
            }

            return true;
        }

        public Visibility EmployeeVisibility
        {
            get { return txtEmployee.Visibility; }
            set { txtEmployee.Visibility = value; tbEmployee.Visibility = value; }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BeginQuery();
        }
        
        public void BeginQuery()
        {
            if (!Validate())
            {
                return;
            }

            if (Query != null)
            {
                Query(this, new DateEventArgs()
                {
                    Begin = (DateTime)dateBegin.SelectedDate,
                    End = (DateTime)dateEnd.SelectedDate,
                    Employee = txtEmployee.Text
                });
            }
        }

        private void dateBegin_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BeginQuery();
            }
        }

        private void txtEmployee_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BeginQuery();
            }
        }

        private void dateEnd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BeginQuery();
            }
        }
    }

    public class DateEventArgs : EventArgs
    {
        public DateTime Begin { get; set; }
        public DateTime End { get; set; }
        public string Employee { get; set; }
    }
}
